import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import dts from 'vite-plugin-dts';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import { lazyImport, VxeResolver } from 'vite-plugin-lazy-import'

// 定义 Rollup 输出配置
const rollupOutputOptions: any = [
  {
    format: 'es',
    // 修改为 false，避免生成过多代码块
    preserveModules: false,
    dir: 'lib/es',
    // entryFileNames: 'wb-platform-library.es',
    assetFileNames: (assetInfo) => {
      if (assetInfo.type === 'asset' && /\.(css)$/i.test(assetInfo.name || "")) {
        const componentName = `wb-platform-library-${(assetInfo.name || 'style.').split('.')[0]}`;
        return `css/${componentName}.css`;
      }
      return 'assets/[name].[ext]';
    }
  },
  {
    format: 'cjs',
    // 修改为 false，避免生成过多代码块
    preserveModules: false,
    dir: 'lib/dist',
    entryFileNames: 'wb-platform-library.cjs',
    assetFileNames: (assetInfo) => {
      if (assetInfo.type === 'asset' && /\.(css)$/i.test(assetInfo.name || "")) {
        const componentName = `wb-platform-library-${(assetInfo.name || 'style.').split('.')[0]}`;
        return `css/${componentName}.css`;
      }
      return 'assets/[name].[ext]';
    }
  }
];

// 定义插件配置
const pluginsConfig = [
  vue(),
  // 生成 TypeScript 声明文件到 es 目录
  dts({ outDir: 'lib/es' }),
  // 生成 TypeScript 声明文件到 dist 目录
  dts({ outDir: 'lib/dist' }),
  {
    name: 'vite:import-css',
    apply: 'build',
    enforce: 'post',
    renderChunk(code, chunk) {
      if (!chunk.isEntry && chunk.type === 'chunk' && /\index.(js)$/i.test(chunk.fileName)) {
        const componentName = `wb-platform-library-${chunk.fileName.split('/')[0]}`;
        return `import './css/${componentName}.css';\n${code}`;
      }
      return code;
    }
  },
  // AutoImport({
  //   resolvers: [ElementPlusResolver()],
  //   imports: ['vue'],
  //   dts: 'types/auto-imports.d.ts'
  // }),
  // Components({
  //   resolvers: [ElementPlusResolver()],
  //   dirs: ['src/components'],
  //   dts: 'types/components.d.ts'
  // }),
  lazyImport({
    resolvers: [
      VxeResolver({
        libraryName: 'vxe-table',
      }),
      VxeResolver({
        libraryName: 'vxe-pc-ui',
      }),
    ],
  })
];

export default defineConfig({
  build: {
    target: 'esnext',
    outDir: 'lib',
    assetsDir: 'assets',
    minify: 'esbuild',
    cssCodeSplit: true,
    rollupOptions: {
      external: ['vue', 'element-plus'],
      // 确保只包含必要的入口文件
      input: ['index.ts', '/packages/style/default.less'],
      output: rollupOutputOptions,
    },
    lib: {
      // 确保只包含必要的入口文件
      entry: ['index.ts'],
      formats: ['es', 'cjs'],
      name: 'wbPlatformLibrary',
      fileName: (format) => `wb-platform-library.${format}.js`
    }
  },
  esbuild: {
    drop: ['console', 'debugger'],
  },
  plugins: pluginsConfig,
  server: {
    port: 3000,
    open: true
  },
  css: {
    preprocessorOptions: {
      less: {
        additionalData: `@import "packages/style/theme.less";`
      }
    }
  }
});